﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;

namespace TAMSUP
{
    /// <summary>
    /// Interaction logic for addRencana.xaml
    /// </summary>
    public partial class addRencana : Window
    {
        string ID;
        Boolean isUbah = false;

        public addRencana(string ID_Penyewaan)
        {
            this.ID = ID_Penyewaan;
            InitializeComponent();
            cancelButton.Content = "Clear";
        }
        public addRencana(string ID_Penyewaan, string ID_RP)
        {
            this.ID = ID_RP;
            isUbah = true;
            InitializeComponent();
            label1.Content = "Edit Rencana";
            addButton.Content = "Edit";
            cancelButton.Content = "Cancel";
            this.Title = "TAMS UP :: Form :: Edit Rencana Pembayaran";
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (isUbah)
            {
                try
                {
                    homeWindow.Con.Open();

                    SqlDataAdapter da = new SqlDataAdapter("select * from RencanaPembayaran where ID_RP = '" + ID + "';", homeWindow.Con);
                    DataSet ubah = new DataSet();
                    da.Fill(ubah, "delete RP");

                    pembke.Text = ubah.Tables["delete RP"].Rows[0].ItemArray.ElementAt(2).ToString();
                    datePicker1.Text = ubah.Tables["delete RP"].Rows[0].ItemArray.ElementAt(3).ToString();
                    jumlah.Text = ubah.Tables["delete RP"].Rows[0].ItemArray.ElementAt(4).ToString();
                    keterangan.Text = ubah.Tables["delete RP"].Rows[0].ItemArray.ElementAt(5).ToString();

                    homeWindow.Con.Close();
                }
                catch (SqlException sqle)
                {
                    MessageBox.Show("Ada kesalahan pada koneksi ke server, silakan periksa jaringan.\n\n SQL error code : " + sqle.State, "Error : Connection Not Established");
                }
            }
            else
            {
                datePicker1.SelectedDate = DateTime.Now;
            }
            
        }

        private void addButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                homeWindow.Con.Open();
                SqlDataAdapter da;
                DataSet ds;
                string _Insert, _Date;
                SqlCommand _cmd;
                _Date = datePicker1.SelectedDate.Value.ToString("M/d/yyyy");

                if (errorChecking())
                {
                    if (isUbah)
                    {
                        _Insert = @"update RencanaPembayaran
                                set Pembayaran_ke = '" + pembke.Text + "', Tanggal = '" + _Date + "', Jumlah = '" + jumlah.Text + "', keterangan = '" + keterangan.Text + "' " +
                                        "where ID_RP = '" + ID + "';";
                        _cmd = new SqlCommand(_Insert, homeWindow.Con);
                        _cmd.ExecuteNonQuery();
                        homeWindow.Con.Close();
                        topWindow.rencpemb.FillData();
                        this.Close();
                    }
                    else
                    {
                        da = new SqlDataAdapter("select * from RencanaPembayaran", homeWindow.Con);
                        ds = new DataSet();
                        da.Fill(ds, "Daftar Semua RP");

                        int count = ds.Tables["Daftar Semua RP"].Rows.Count + 1;
                        string tempID_RP = "Z" + count;
                        _Insert = @"insert into RencanaPembayaran (ID_RP, ID_Penyewaan, Pembayaran_ke, Tanggal, Jumlah, Keterangan)
                                      Values('" + tempID_RP + "','" + ID + "','" + pembke.Text + "','" + _Date + "','" +
                                                 jumlah.Text + "','" + keterangan.Text + "')";

                        _cmd = new SqlCommand(_Insert, homeWindow.Con);
                        _cmd.ExecuteNonQuery();
                        homeWindow.Con.Close();
                        topWindow.rencpemb.FillData();
                        restart();
                    }
                }
            }
            catch (SqlException sqle)
            {
                MessageBox.Show("Ada kesalahan pada koneksi ke server, silakan periksa jaringan.\n\n SQL error code : " + sqle.State, "Error : Connection Not Established");
            }
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            if (isUbah)
            {
                this.Close();
            }
            else
            {
                pembke.Text = "";
                jumlah.Text = "";
                keterangan.Text = "";
                datePicker1.SelectedDate = DateTime.Now;
            }
        }

        private void restart()
        {
            Brush validColor = new SolidColorBrush(Color.FromRgb(200, 200, 200));
            pembke.Text = "";
            pembke.Background = validColor;
            jumlah.Text = "";
            jumlah.Background = validColor;
            keterangan.Text = "";
            keterangan.Background = validColor;
            datePicker1.SelectedDate = DateTime.Now;
        }

        private Boolean errorChecking()
        {
            Boolean valid = true;
            Brush invalidColor = new SolidColorBrush(Color.FromRgb(255, 225, 0));
            Brush ValidColor = new SolidColorBrush(Color.FromRgb(200, 200, 200));
            int temp;
            if (pembke.Text == "" || !Int32.TryParse(pembke.Text.ToString(), out temp))
            {
                pembke.Background = invalidColor;
                if (valid) valid = false;
            }
            else
            {
                pembke.Background = ValidColor;
            }
            if (jumlah.Text == "" || !Int32.TryParse(jumlah.Text.ToString(), out temp))
            {
                jumlah.Background = invalidColor;
                if (valid) valid = false;
            }
            else
            {
                jumlah.Background = ValidColor;
            }
            if (!valid)
            {
                MessageBox.Show("Ada kesalahan pengisian informasi tambah rencana pembayaran", "Invalid Information");
            }
            return valid;
        }

        private void datePicker1_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}
